#pragma once
#include <iostream>
#include <fstream>
#include <string>

class Util
{
public:
    Util()
    {
    }
    static bool ReadFileContent(const std::string &filename, std::string *out)
    {
        //默认以文本文件读取
        // std::ifstream in(filename);
        // if(!in.is_open())
        // {
        //      return false;
        // }
        //  std::string line;
        // while(std::getline(in,line))
        // {
        //     *out+=line;
        // }
        // in.close();
         // version2 : 以二进制方式进行读取
        int filesize = FileSize(filename);
        if(filesize > 0)
        {
            std::ifstream in(filename);
            if(!in.is_open())
                return false;
            out->resize(filesize);
            in.read((char*)(out->c_str()), filesize);
            in.close();
        }
        else
        {
            return false;
        }

        return true;
    }
    static bool ReadOneLine(std::string& reqstr, std::string *reqline, const std::string &glinespace)
    {
        auto e=reqstr.find(glinespace);
        if(e==std::string::npos)
        {
            return false;
        }
        *reqline=reqstr.substr(0,e);
        reqstr.erase(0, e + glinespace.size());
        return true;
    }
     static int FileSize(const std::string &filename)
     {
         std::ifstream in(filename, std::ios::binary);//以二进制方式打开文件
         if(!in.is_open())
            return -1;
        in.seekg(0,in.end);
        int Size=in.tellg();
        in.seekg(0,in.beg);
         in.close();
        return Size;
     }
    ~Util()
    {
    }

private:
};